package com.sync.syncmasterdata.product.service.convert;

import com.sync.syncmasterdata.configuration.ConfInfo;
import com.sync.syncmasterdata.customer.service.http.HttpAPIServiceForCrmSync;
import com.sync.syncmasterdata.product.entity.BPM.InventoryForItemSheetEntity;
import com.sync.syncmasterdata.product.pojo.crm.InventoryForCrm;
import com.sync.syncmasterdata.product.service.ConvertInterface;
import com.sync.syncmasterdata.util.NormalUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ProductBPMtoCRM implements ConvertInterface<InventoryForItemSheetEntity, InventoryForCrm> {


    @Autowired
    private ConfInfo confInfo;
    @Autowired
    private HttpAPIServiceForCrmSync httpAPIService;


    @Override
    public InventoryForCrm convertTo(InventoryForItemSheetEntity bpm) {
        InventoryForCrm crm = new InventoryForCrm();
        crm.setNew_add_code("/");
        /**
         * 可用量
         */
        crm.setNew_available(0.00);
        crm.setNew_end_date(null);
        /**
         * u8id
         */
        crm.setNew_code(bpm.getInventory_code_txt());
        /**
         * 是否常规备货
         */
        crm.setNew_isnormal(true);
        /**
         * 基本单位 需转换
         */
        String unitid=unitToCrmUnit("038");
        if (unitid != null) {
            crm.setNew_main_measure(String.format("/new_units(%s)", unitid));
            crm.setNew_sale_measure(String.format("/new_units(%s)", unitid));
        }else{
            crm.setNew_main_measure(null);
            crm.setNew_sale_measure(null);
        }
        crm.setNew_name(bpm.getProduct_name());
        crm.setNew_saunit_rate(13.0f);
        crm.setNew_sort_code("JC04");
        crm.setNew_specs(null);
        crm.setNew_taxrate(13.00d);
        /**
         * 库位 中文
         */
        crm.setNew_warehouse_type("检测项目库");
        /**
         * 品牌
         */
        crm.setNew_brand(bpm.getBrand());
        /**
         * 生产企业 oa生产企业文本
         */
        crm.setNew_enterprise(bpm.getBrand());
        crm.setNew_domestic_or_imported(0);
        crm.setNew_business_type(bpm.getBusiness_type());
        crm.setNew_line(bpm.getProduct_line());
        crm.setNew_group(bpm.getProduct_group());
        crm.setNew_project(bpm.getItem_name_txt());
        return crm;
    }
    /**
     * 转换单位 to crmGUID
     */
    public String unitToCrmUnit(String unitCode) {
        try {
            //增加编码
            String path = NormalUtil.encodeUrl(String.format("%s%s", confInfo.getCrmurl(), "new_units?$filter=new_code eq '" + unitCode + "'"));

            String responseStr = httpAPIService.doGetCrm(path);
            JSONObject jsonObject = new JSONObject(responseStr);
            log.info("CRM单位换算:" + path);
            JSONArray jsonArray = jsonObject.getJSONArray("value");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = (JSONObject) jsonArray.get(i);
                return json.get("new_unitid").toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return null;
    }
}
